#ifndef DETECTIONWINDOW_H
#define DETECTIONWINDOW_H

#include <QWidget>
#include <QChartView>
#include <QtCharts/QPieSeries>
#include <QtCharts/QPieSlice>
#include <QLabel>
#include <QPainter>
#include <QPropertyAnimation>
#include <QPainterPath>
#include <QPen>
#include <QBrush>
#include <QFont>
#include <QTimer>
#include <QLayout>
#include <QGridLayout>
#include <QCoreApplication>
#include "qcustomplot.h"

QT_CHARTS_USE_NAMESPACE
namespace Ui {
class DetectionWindow;
}

class AqiCenterWidget : public QWidget {
    Q_OBJECT
    Q_PROPERTY(qreal progress READ progress WRITE setProgress NOTIFY progressChanged)

public:
    //标签数字
    QLabel* lab;

    explicit AqiCenterWidget(QWidget *parent = nullptr);

    qreal progress() const {
        return m_progress;
    }
    int getAqiValue();

public slots://操作饼状图进度条的角度
    void setProgress(double progress) {//传输进来进度
        if (m_progress != progress) {//如果进度需要更新
            //用qBound确保数值范围只能在0~1之间,如果大于1,变为1
            m_progress = qBound(0.0, progress, 1.0);
            //发出信号
            emit progressChanged(m_progress);
            //更新标签,设置标签范围是0~500
            QString str=QString("%1").arg(m_progress*1000);
            qDebug()<<str;
            lab->setText(str);

            update(); // 触发控件更新
        }
    }

signals:
    void progressChanged(qreal progress);

protected:
    void paintEvent(QPaintEvent *event);

private:
    qreal m_progress;//操作进度条角度从0~1为进度
};

class DetectionWindow : public QWidget
{
    Q_OBJECT

public:
    explicit DetectionWindow(QWidget *parent = 0);
    ~DetectionWindow();

    static int AQIvalue;

    QVBoxLayout *vLayout;
    QTimer dataTimer;
    QCustomPlot *customPlot;
    QCustomPlot *customPlot1; //柱状图
    QCustomPlot *customPlot2; //柱状图加折线图

    void initWindow();
    int getPm25Value();
    int getPm10Value();
    int getTemperatureValue();
    int getHumidityValue();
    int getWindSpeedValue();
    int getPHValue();

    void AqiPieChart();

    void setupBarChartDemo(QCustomPlot *customPlot);//柱状图
    void setupRealtimeDataDemo(QCustomPlot *customPlot); //折线图
    void setupStyledDemo(QCustomPlot *customPlot);


private slots:
    void updateData();
    void realtimeDataSlot();

private:
    Ui::DetectionWindow *ui;
    AqiCenterWidget *aqiCenterWidget;
    QLabel *pm25ValueLabel;
    QLabel *pm10ValueLabel;
    QLabel *temperatureValueLabel;
    QLabel *humidityValueLabel;
    QLabel *windSpeedValueLabel;
    QLabel *phValueLabel;
    QString demoName;

    QCPBars *regen = nullptr;

    QChartView* chartView;
};

#endif // DETECTIONWINDOW_H
